<?php
#!/usr/bin/php
    header("Content-Type: text/html; charset=utf-8") ;
    set_time_limit(0);
    date_default_timezone_set('PRC'); 
    error_reporting(E_ALL);
     echo 1;
    $con = mysql_connect("101.37.76.105","root","eaifxwaihui");
    print_r($con);
    if (!$con) { die('Could not connect: ' . mysql_error());  }
     print_r(mysql_error());
    mysql_select_db("test", $con);
    echo 2;
    exit();
    //定时执行
    for ( $i = 0;$i < 120;$i++){
        echo $i;
        //查看会员到期还有订阅记录的用户信息
        $query = "select m.id,m.tel,m.expire_time from waihui_member as m  where (m.isvip = '1' or m.isvip = '3') and (m.expire_time <=".time().' OR m.expire_time is NULL)';//
       
        $list  = mysql_query($query);
        if($list){
            while($lval = mysql_fetch_assoc($list)) { //循环用户信息
                //用户订阅的记录
                $followsql = 'select f.id,f.analyst_id,f.direction,m.tel from waihui_follow as f inner join waihui_member as m on f.analyst_id=m.id where status = 1 and follower_id='.$lval['id'];
                $followdata= mysql_query($followsql);
                //循环订阅记录信息
                while($fval = mysql_fetch_assoc($followdata)) {
                    //查询订阅的此分析师下所有的未平仓订单
                    $ordersql  = 'select tradeid,tel from waihui_order where isstop = 1 and ispersonal = 0 and tel=' . $fval['tel'];
                    $orderdata = mysql_query($ordersql);
                    if ($orderdata) { //如果有未平仓订单
                        //循环未平仓订单
                        while ($oval = mysql_fetch_assoc($orderdata)) {
                            $tradeid      = $oval['tradeid']; //订单号
                            $analyst_tel  = $oval['tel']; //分析师手机号
                            $follower_tel = $lval['tel']; //用户手机号

                            $host   = "101.37.76.105";
                            $port   = 8099;
                            //创建一次 实现多次通信
                            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("cannot create socket\n");
                            $conn   = socket_connect($socket, $host, $port) or die("cannot connect server\n");
                            socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 10);
                            if($conn != 'cannot connect server\n') { //连接成功
                                $infodata = "{   
                                head: {
                                  account: '$analyst_tel',
                                  tradecode: 8803,
                                  allsend: '$follower_tel'
                                },
                                body: {
                                    fromsource: 'netpage',
                                    trader_orderid : '$tradeid',
                                }
                             }&
                             ";
                                //发送平仓报文
                                socket_write($socket, $infodata) or die("cannot write data\n");
                                $infoResult = socket_read($socket, 1024, PHP_BINARY_READ);
                                $infoSubstr = substr($infoResult, 0, (strlen($infoResult) - 1));
                                $info       = json_decode($infoSubstr, true);

                                file_put_contents(date('Y-m-d') . "expoclose.txt", '平仓时间：' . date('Y-m-d H:i:s') . $infodata . '返回报文状态值：' . $info['ret_code'], FILE_APPEND);
                            }else{
                                file_put_contents(date('Y-m-d') . "socketfial.txt", '平仓连接失败时间：' . date('Y-m-d H:i:s').$infodata, FILE_APPEND);
                            }
                        }
                    }
                    $host   = "101.37.76.105";
                    $port   = 8099;
                    //创建一次 实现多次通信
                    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("cannot create socket\n");
                    $conn   = socket_connect($socket, $host, $port) or die("cannot connect server\n");
                    socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 10);
                    if($conn != 'cannot connect server\n') { //连接成功
                        //发送取消订阅报文
                        $id           = $fval['id']; //订阅表里记录的ID
                        $follower_tel = $lval['tel'];//用户手机号
                        $analyst_tel  = $fval['tel'];//分析师手机号
                        $status       = 0;//取消订阅
                        $direct       = 0;//订阅方向
                        $dingdata     = "{   
                      head: {
                        account: '$follower_tel',
                        tradecode: 8804
                      },
                      body: {
                        id: '$id',
                        analyst_tel: '$analyst_tel',
                        follower_tel: '$follower_tel',
                        status :'$status',
                        direct :'$direct' 
                      }
                   }&
                   ";

                        socket_write($socket, $dingdata) or die("cannot write data\n");
                        $dingResult = socket_read($socket, 1024, PHP_BINARY_READ);
                        $dingSubstr = substr($dingResult, 0, (strlen($dingResult) - 1));
                        $dinginfo   = json_decode($dingSubstr, true);
                        if($dinginfo['ret_code'] == '0000') { //报文发送成功
                            //修改订阅状态
                            $updatefollow = "update waihui_follow set status=0  and update_time ='".strtotime('now')."'   where id=$id";
                            $upfresult    = mysql_query($updatefollow);
                        }
                        file_put_contents(date('Y-m-d') . "expfclose.txt", '取消订阅时间：' . date('Y-m-d H:i:s') . $dingdata . '返回报文状态值：' . $dinginfo['ret_code'].'，修改订阅：'.$updatefollow, FILE_APPEND);
                    }else{
                        file_put_contents(date('Y-m-d') . "socketfial.txt", '取消订阅连接失败时间：' . date('Y-m-d H:i:s').$dingdata, FILE_APPEND);
                    }
                }
                //修改会员状态
                $updatemem    = "update waihui_member set isvip='0' where tel='".$lval['tel']."'";
                $upmresult    = mysql_query($updatemem);
                file_put_contents(date('Y-m-d') . "expupmem.txt", '数据库修改：' . date('Y-m-d H:i:s') .$updatemem, FILE_APPEND);
            }
        }
        echo 2;
        usleep(500000);//暂停时间（单位为微秒） 0.5s
    }
    mysql_close($con);
